//
//  LJMRank_sonerViewController.m
//  wind
//
//  Created by 李金明 on 16/1/11.
//  Copyright © 2016年 李金明. All rights reserved.
//

#import "LJMRank_sonerViewController.h"
#import "LJMRank_sonerModel.h"
#import "LJMRank_sonerCollectionViewCell.h"
#import "LJMRankSub_sonerViewController.h"

//barbutton 左面的坐标
#define LEFTBARBUTTONHEIGHT 44
#define LEFTBARBUTTONWIDTH 50

//collectionfowl的坐标
#define COLLECTIONFOWLEDGE 5//边界
#define COLLECTWIDTH 20
@interface LJMRank_sonerViewController () <UICollectionViewDelegate, UICollectionViewDataSource, backNavigationDelegate>
- (void)backNavigationDelegate;
@property (nonatomic, retain) UIButton *leftBarButton;
@property (nonatomic, retain) UICollectionView *collectionV;
@property (nonatomic, retain) NSArray *urlArr;
@property (nonatomic, retain) UIButton *bactButton;
@property (nonatomic, retain) UIView *barview;
@property (nonatomic, retain) UILabel *titleLab;
@property (nonatomic, retain) UIActivityIndicatorView *testActivityIndicator;
@property (nonatomic, retain) UIApplication *application;
@end

@implementation LJMRank_sonerViewController
- (void)dealloc
{
    self.backDelegate = nil;
    [_testActivityIndicator release];
    [_application release];
    [_titleLab release];
    [_bactButton release];
    [_barview release];
    [_urlArr release];
    [_collectionV release];
    [_dataArr release];
    [_leftBarButton release];
    [super dealloc];
}
- (void)setDataArr:(NSMutableArray *)dataArr
{
    if (_dataArr != dataArr) {
        [_dataArr release];
        _dataArr = [dataArr retain];
    }
    [self.collectionV reloadData];
    [self.testActivityIndicator stopAnimating];
    self.application.networkActivityIndicatorVisible = NO;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    //添加navigationBar
    [self addNavigationBar];
    //添加collectionView
    [self addcollectionView];
    //点击网址
    [self didUrl];
    //添加菊花转
    [self addUIActivityIndicatorView];
}
#pragma 点击网址
- (void)didUrl
{
    //单点进去的网址
    self.urlArr = [NSArray arrayWithObjects:
                   @"http://api.dongting.com/misc/singer/top?app=ttpod&v=v8.1.3.2015112716&user_id=0&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=7.0.4&active=1&net=2&openudid=1389d9dc18cb741587d90a5aead8646421f95dc0&idfa=C9BC818C-A0F5-4F04-8F8D-841910E08DF7&utdid=VmGiKEXHDQ0DAIL2FOnFFzxV&alf=201200&bundle_id=com.ttpod.music&latitude=&longtitude= HTTP/1.1",
                   @"http://api.dongting.com/misc/singer/hot?app=ttpod&v=v8.1.3.2015112716&user_id=0&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=7.0.4&active=1&net=2&openudid=1389d9dc18cb741587d90a5aead8646421f95dc0&idfa=C9BC818C-A0F5-4F04-8F8D-841910E08DF7&utdid=VmGiKEXHDQ0DAIL2FOnFFzxV&alf=201200&bundle_id=com.ttpod.music&latitude=&longtitude= HTTP/1.1",
                   @"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=47&size=1000&page=1&app=ttpod&v=v8.1.3.2015112716&user_id=0&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=7.0.4&active=1&net=2&openudid=1389d9dc18cb741587d90a5aead8646421f95dc0&idfa=C9BC818C-A0F5-4F04-8F8D-841910E08DF7&utdid=VmGiKEXHDQ0DAIL2FOnFFzxV&alf=201200&bundle_id=com.ttpod.music&latitude=&longtitude= HTTP/1.1",
                   @"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=48&size=1000&page=1&app=ttpod&v=v8.1.3.2015112716&user_id=0&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=7.0.4&active=1&net=2&openudid=1389d9dc18cb741587d90a5aead8646421f95dc0&idfa=C9BC818C-A0F5-4F04-8F8D-841910E08DF7&utdid=VmGiKEXHDQ0DAIL2FOnFFzxV&alf=201200&bundle_id=com.ttpod.music&latitude=&longtitude= HTTP/1.1",
                   @"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=49&size=1000&page=1&app=ttpod&v=v8.1.3.2015112716&user_id=0&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=7.0.4&active=1&net=2&openudid=1389d9dc18cb741587d90a5aead8646421f95dc0&idfa=C9BC818C-A0F5-4F04-8F8D-841910E08DF7&utdid=VmGiKEXHDQ0DAIL2FOnFFzxV&alf=201200&bundle_id=com.ttpod.music&latitude=&longtitude= HTTP/1.1",
                   @"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=249&size=1000&page=1&app=ttpod&v=v8.1.3.2015112716&user_id=0&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=7.0.4&active=1&net=2&openudid=1389d9dc18cb741587d90a5aead8646421f95dc0&idfa=C9BC818C-A0F5-4F04-8F8D-841910E08DF7&utdid=VmGiKEXHDQ0DAIL2FOnFFzxV&alf=201200&bundle_id=com.ttpod.music&latitude=&longtitude= HTTP/1.1",
                   @"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=250&size=1000&page=1&app=ttpod&v=v8.1.3.2015112716&user_id=0&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=7.0.4&active=1&net=2&openudid=1389d9dc18cb741587d90a5aead8646421f95dc0&idfa=C9BC818C-A0F5-4F04-8F8D-841910E08DF7&utdid=VmGiKEXHDQ0DAIL2FOnFFzxV&alf=201200&bundle_id=com.ttpod.music&latitude=&longtitude= HTTP/1.1",
                   @"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=51&size=1000&page=1&app=ttpod&v=v8.1.3.2015112716&user_id=0&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=7.0.4&active=1&net=2&openudid=1389d9dc18cb741587d90a5aead8646421f95dc0&idfa=C9BC818C-A0F5-4F04-8F8D-841910E08DF7&utdid=VmGiKEXHDQ0DAIL2FOnFFzxV&alf=201200&bundle_id=com.ttpod.music&latitude=&longtitude= HTTP/1.1",
                   @"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=251&size=1000&page=1&app=ttpod&v=v8.1.3.2015112716&user_id=0&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=7.0.4&active=1&net=2&openudid=1389d9dc18cb741587d90a5aead8646421f95dc0&idfa=C9BC818C-A0F5-4F04-8F8D-841910E08DF7&utdid=VmGiKEXHDQ0DAIL2FOnFFzxV&alf=201200&bundle_id=com.ttpod.music&latitude=&longtitude= HTTP/1.1",
                   @"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=252&size=1000&page=1&app=ttpod&v=v8.1.3.2015112716&user_id=0&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=7.0.4&active=1&net=2&openudid=1389d9dc18cb741587d90a5aead8646421f95dc0&idfa=C9BC818C-A0F5-4F04-8F8D-841910E08DF7&utdid=VmGiKEXHDQ0DAIL2FOnFFzxV&alf=201200&bundle_id=com.ttpod.music&latitude=&longtitude= HTTP/1.1",
                   @"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=53&size=1000&page=1&app=ttpod&v=v8.1.3.2015112716&user_id=0&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=7.0.4&active=1&net=2&openudid=1389d9dc18cb741587d90a5aead8646421f95dc0&idfa=C9BC818C-A0F5-4F04-8F8D-841910E08DF7&utdid=VmGiKEXHDQ0DAIL2FOnFFzxV&alf=201200&bundle_id=com.ttpod.music&latitude=&longtitude= HTTP/1.1",nil];
}
#pragma 增加个菊花转
- (void)addUIActivityIndicatorView
{
    //    //增加个菊花转
    self.testActivityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    self.testActivityIndicator.backgroundColor = [UIColor blackColor];
    self.testActivityIndicator.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - 60) / 2 , [UIScreen mainScreen].bounds.size.height / 2 - 100, 60, 60);
    self.testActivityIndicator.layer.cornerRadius = 10;
    self.testActivityIndicator.layer.masksToBounds = YES;
    [self.barview addSubview:self.testActivityIndicator];
    [_testActivityIndicator release];
    [self.testActivityIndicator startAnimating];
    //增加网络旁边菊花转的方法
    self.application = [UIApplication sharedApplication];
    self.application.networkActivityIndicatorVisible = YES;
    
}
#pragma 添加collectionView
- (void)addcollectionView
{
    UICollectionViewFlowLayout *fowl = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    fowl.sectionInset = UIEdgeInsetsMake(COLLECTIONFOWLEDGE + COLLECTIONFOWLEDGE, COLLECTIONFOWLEDGE, COLLECTIONFOWLEDGE, COLLECTIONFOWLEDGE);
    fowl.itemSize = CGSizeMake((self.view.frame.size.width - COLLECTWIDTH) / 3, (self.view.frame.size.width - COLLECTWIDTH - COLLECTIONFOWLEDGE - COLLECTIONFOWLEDGE) / 3 + COLLECTWIDTH);
    fowl.minimumLineSpacing = COLLECTIONFOWLEDGE;
    fowl.minimumInteritemSpacing = COLLECTIONFOWLEDGE;
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - self.navigationController.navigationBar.frame.size.height - COLLECTWIDTH - COLLECTIONFOWLEDGE - COLLECTIONFOWLEDGE) collectionViewLayout:fowl];
   
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    self.collectionV.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.collectionV];
    [_collectionV release];
     [self.collectionV registerClass:[LJMRank_sonerCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
}
#pragma 添加navigationBar
- (void)addNavigationBar
{
    self.barview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, NAVIGATIONBARHEIGHT)];
    self.barview.userInteractionEnabled = YES;
    self.barview.backgroundColor = [UIColor colorWithRed:69 / 255.0 green:175 / 255.0 blue:248 / 255.0 alpha:1.0];
    [self.navigationController.navigationBar addSubview:self.barview];
    [_barview release];
    self.bactButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.bactButton setTitle:@"返回" forState:UIControlStateNormal];
    [self.bactButton addTarget:self action:@selector(barAction) forControlEvents:UIControlEventTouchUpInside];
    [self.bactButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.barview addSubview:self.bactButton];
    [self.bactButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.barview.mas_left).offset(10);
        make.width.mas_equalTo(@50);
        make.bottom.equalTo(self.barview.mas_bottom).offset(-5);
        make.height.mas_equalTo(@30);
    }];
    self.titleLab = [[UILabel alloc] init];
    self.titleLab.text = @"歌手";
    self.titleLab.textAlignment = NSTextAlignmentCenter;
    self.titleLab.font = [UIFont boldSystemFontOfSize:20];
    self.titleLab.textColor = [UIColor whiteColor];
    [self.barview addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.barview.mas_centerX);
        make.width.mas_equalTo(@50);
        make.centerY.equalTo(self.bactButton.mas_centerY);
        make.height.mas_equalTo(@30);
    }];


    
    
}
#pragma barAction上的返回动作
- (void)barAction
{
    [self.navigationController popViewControllerAnimated:YES];
    [self.barview removeFromSuperview];
    [self.backDelegate backRankAction];
    [self.testActivityIndicator stopAnimating];
    self.application.networkActivityIndicatorVisible = NO;


}
#pragma collectionView的协议方法
#pragma 个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
#pragma cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LJMRank_sonerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    LJMRank_sonerModel *model = nil;
    if (nil != self.dataArr) {
      model = [self.dataArr objectAtIndex:indexPath.row];
    }



    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.pic_url] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
    cell.nameL.text = model.title;
    return cell;

}
#pragma 点击的动作
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = [self.urlArr objectAtIndex:indexPath.row];
    LJMRank_sonerModel *model = [self.dataArr objectAtIndex:indexPath.row];

    LJMRankSub_sonerViewController *rankSubVC = [[LJMRankSub_sonerViewController alloc] init];
    rankSubVC.backDelegate = self;
    [self.navigationController pushViewController:rankSubVC animated:YES];
    [self.barview removeFromSuperview];
    rankSubVC.navigationItem.hidesBackButton = YES;
    rankSubVC.urlStr = str;
    rankSubVC.titleStr = model.title;
    [rankSubVC release];
    

}
#pragma 协议方法加上Navigate
- (void)backNavigationDelegate
{
    [self addNavigationBar];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
